#!/usr/bin/env python                         # -*- coding: utf-8 -*-

"""Utility functions for the IQ software."""

import os
import pkg_resources
import simplejson
import sys

_dirs = [ os.path.join(*x) for x in
          [ (os.environ.get('IQBINARIES', ''),),
            (os.path.expanduser('~'), '.iq', sys.platform),
            (os.path.expanduser('~'), '.iq'),
            (sys.exec_prefix, 'lib', 'IQ', sys.platform),
            (sys.exec_prefix, 'lib', 'IQ'),
            (sys.prefix, 'lib', 'IQ', sys.platform),
            (sys.prefix, 'lib', 'IQ'),
            ]]

def find_executable(name):
    """Return the pathname of the given backend executable."""

    for dir in _dirs:
        path = os.path.join(dir, name)
        if os.path.exists(path):
            return path

    errmsg = "Cannot find executable '%s' in any of %s" \
        % (name, _dirs)
    raise IOError, errmsg

def commands_json():
    """Return the contents of commands_json."""

    return pkg_resources.resource_string(__name__, 'commands.json')

class Struct(object):
    def __init__(self, **kw):
        self.__dict__.update(kw)
